﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using Sale_Model;
using SaleSystem;
using ExtAspNet;

namespace SaleSystem.Fina
{
    public partial class frm_RecordEdit : PageBase
    {
        #region Page Event
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl();
                BindDropDownList();

                GetAndBind();

            }
        }
        protected void bnSave_Click(object sender, EventArgs e)
        {

            SModel.CreateMan = LinkService.GetCurrentUser().SUser_Id;
            SModel.CreateTime = System.DateTime.Now;
            SModel.RList_BData_Id = ValueHandler.GetIntNumberValue(this.ddl_RList_BData_Id.SelectedValue);
            SModel.RList_Code = this.tb_RList_Code.Text;
            SModel.RList_Des = this.tb_RList_Des.Text;
            SModel.RList_EndTime = DateTime.Parse(dp_RList_EndTime.Text);
            SModel.RList_Id = this.Id;
            SModel.RList_Money = ValueHandler.GetDecNumberValue( this.nb_RList_Money.Text );
            SModel.RList_StartTime = DateTime.Parse(dp_RList_StartTime.Text);
            SModel.RList_Title = this.tb_RList_Title.Text;
            SModel.State = 1;
            if (SModel.RList_EndTime.ToString("yyyy-MM") != SModel.RList_StartTime.ToString("yyyy-MM"))
            {
                ExtAspNet.Alert.Show("开始日期同结束日期必须在同一月内！", "操作提示！", MessageBoxIcon.Information);
                return;
            }
            bool blHasExists = Manager.HasExists(SModel);
            if (blHasExists)
            {
                ExtAspNet.Alert.Show("该支出单号已经存在，请重新输入！", MessageBoxIcon.Information);
            }
            else
            {
                bool blRes = Manager.Save(SModel);
                if (blRes)
                {
                    //ExtAspNet.PageContext.RegisterStartupScript("parent.__doPostBack('','"+LinkService.RefreshString+"')");
                    // ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.
                    //ExtAspNet.Alert.Show("OK","OK", ExtAspNet.ActiveWindow.GetHidePostBackReference(LinkService.RefreshString));
                    ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference(LinkService.RefreshString));

                }
                else
                    ExtAspNet.Alert.Show("支出单编辑失败！", MessageBoxIcon.Error);
            }
        } 
        #endregion

        #region Method
        private void InitControl()
        {
            bnClose.OnClientClick = ExtAspNet.ActiveWindow.GetHideReference();
            if (Request["Id"] != null)
            {
                Id = ValueHandler.GetIntNumberValue(Request["Id"]);
            }

        }
        
        private void BindDropDownList()
        {

            BaseDataOp BaseManager = new BaseDataOp();
            DataTable dtType = BaseManager.GetList("BData_Type=1");
            ddl_RList_BData_Id.DataSource = dtType;
            ddl_RList_BData_Id.DataBind();
            ddl_RList_BData_Id.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));

        }
        private void GetAndBind()
        {
            if (Id != 0)
            {
                SModel.RList_Id = this.Id;
                SModel = Manager.GetModel(SModel) as RecordList;
                this.tb_RList_Code.Text = SModel.RList_Code;
                this.tb_RList_Des.Text = SModel.RList_Des;
                this.tb_RList_Title.Text = SModel.RList_Title;
                this.nb_RList_Money.Text = SModel.RList_Money.ToString();
                this.dp_RList_EndTime.Text = SModel.RList_EndTime.ToString("yyyy-MM-dd");
                this.dp_RList_StartTime.Text = SModel.RList_StartTime.ToString("yyyy-MM-dd");
                this.ddl_RList_BData_Id.SelectedValue = SModel.RList_BData_Id.ToString();
            }
            else
            {
                this.tb_RList_Code.Text = Coder.CreateCoder(EMenuList.费用_支出);
                this.dp_RList_EndTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                this.dp_RList_StartTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
            }

        }
        #endregion

        #region Property
        RecordListOp Manager = new RecordListOp();

        new Sale_Model.RecordList SModel = new Sale_Model.RecordList();
        #endregion
    }
}
